package com.lframework.xingyun.basedata.impl.product;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lframework.starter.web.core.impl.BaseMpServiceImpl;
import com.lframework.starter.web.core.utils.OpLogUtil;
import java.io.Serializable;
import com.lframework.starter.web.core.annotations.oplog.OpLog;
import com.lframework.starter.web.inner.components.oplog.OtherOpLogType;
import com.lframework.xingyun.basedata.entity.Product;
import com.lframework.xingyun.basedata.entity.ProductPropertyItem;
import com.lframework.xingyun.basedata.entity.ProductSpec;
import com.lframework.xingyun.basedata.mappers.ProductPropertyItemMapper;
import com.lframework.xingyun.basedata.mappers.ProductSpecMapper;
import com.lframework.xingyun.basedata.service.product.ProductService;
import com.lframework.xingyun.basedata.service.product.ProductSpecCostDetailService;
import com.lframework.xingyun.basedata.service.product.ProductSpecService;
import com.lframework.xingyun.basedata.vo.product.info.ProductSpecSaveVo;
import com.lframework.xingyun.basedata.vo.product.spec.ProductSpecVo;
import com.lframework.xingyun.basedata.vo.product.spec.QueryProductSpecVo;
import com.lframework.xingyun.basedata.vo.product.speccost.ProductSpecCostDetailVo;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class ProductSpecServiceImpl extends BaseMpServiceImpl<ProductSpecMapper, ProductSpec> implements ProductSpecService {


    @Override
    public List<ProductSpec> query(QueryProductSpecVo vo) {
        return getBaseMapper().query(vo);
    }

    @Cacheable(value = ProductSpec.CACHE_NAME, key = "@cacheVariables.tenantId() + #id", unless = "#result == null")
    @Override
    public ProductSpec findById(String id) {

        return getBaseMapper().selectById(id);
    }

    @OpLog(type = OtherOpLogType.class, name = "保存商品规格表，productId：{}", params = {"#productId"})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public String save(String productId, List<ProductSpecVo> vo) {

        getThis(ProductSpecService.class).remove(Wrappers.lambdaUpdate(ProductSpec.class)
                .eq(ProductSpec::getProductId, productId));

        List<ProductSpec> list = new ArrayList<>();
        for (ProductSpecVo productSpecVo : vo) {
            ProductSpec data = new ProductSpec();
            data.setProductId(productId);
            data.setPropertyId(productSpecVo.getPropertyId());
            data.setItemId(productSpecVo.getItemId());
            data.setPurchasePrice(productSpecVo.getPurchasePrice());
            data.setRetailPrice(productSpecVo.getRetailPrice());
            data.setSalePrice(productSpecVo.getSalePrice());
            data.setAvailable(productSpecVo.getAvailable());
            list.add(data);
        }

        saveBatch(list);

        List<ProductSpecCostDetailVo> costDetailVos = new ArrayList<>();
        for (ProductSpecVo productSpecVo : vo) {
            if (CollUtil.isNotEmpty(productSpecVo.getCostDetailData())) {
                costDetailVos.addAll(productSpecVo.getCostDetailData());
            }
        }

        getThis(ProductSpecCostDetailService.class).save(productId, costDetailVos);


        OpLogUtil.setVariable("productId", productId);
        OpLogUtil.setExtra(vo);

        return productId;
    }

    @Override
    public String saveSpec(String productId, List<ProductSpecSaveVo> vo) {

        return productId;
    }

    @CacheEvict(value = ProductSpec.CACHE_NAME, key = "@cacheVariables.tenantId() + #key")
    @Override
    public void cleanCacheByKey(Serializable key) {

    }
}

